// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-07 20:27
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.DateTrain;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ConvertToBase7 {
    public String convertToBase7(int num) {
        // 将10进制数字转化为7进制数字
        // 49,7,1 -> 逢七进一
        boolean isNegative = num < 0;
        if (isNegative) {
            num = num * -1;
        }
        StringBuilder builder = new StringBuilder();
        int sum = 0;
        int n = 0;
        while (Math.pow(7, n + 1) < num) {
            n++;
        }
        // 多设一位，防止溢出
        int[] nums = new int[n + 2];
        while (n >= 0 && sum != num) {
            sum += Math.pow(7, n);
            nums[n]++;
            if (sum > num) {
                sum -= Math.pow(7, n);
                nums[n]--;
                n--;
            }
        }

        for (int i = 0; i <= nums.length - 1; i++) {
            if (nums[i] >= 7) {
                nums[i + 1] += nums[i] / 7;
                nums[i] %= 7;
            }
            if (i == nums.length - 1 && nums[i] == 0) {
                break;
            }
            builder.append(nums[i]);
        }
        if (isNegative) {
            builder.append('-');
        }
        return builder.reverse().toString();
    }

    @Test
    public void shout() {
        System.out.println(convertToBase7(-7));
    }

    @Test
    public void shout00() {
        String SENTENCE = "Hello jack" + "Where you living now" + "how do you do";
        List<Character> ans = IntStream.range(0, SENTENCE.length()).mapToObj(SENTENCE::charAt).collect(Collectors.toList());
        List<Integer> res = IntStream.range(0, 10).mapToObj(Integer::new).collect(Collectors.toList());


    }

}
